package cc.eddic.practice.concurrency;

import java.util.Collection;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import cc.eddic.practice.concurrency.impl.LogParserWithLoopImpl;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@Slf4j
@SpringBootApplication

public class ConcurrencyWqjApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(ConcurrencyWqjApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        long start = System.currentTimeMillis();
        log.info("start");
//        loop();
        //executor();
        log.info("elapsed {} ms", System.currentTimeMillis() - start);

    }

    private void executor() {
        ExecutorService executorService = Executors.newFixedThreadPool(16);
        CountDownLatch latch = new CountDownLatch(L0G_FILE_COUNT);
        for (int i = 0; i < L0G_FILE_COUNT; i++) {
            int finalI = i;
            executorService.submit(() -> dummyParse(finalI, latch));
        }

        if (log.isInfoEnabled()) log.info("latch await");

        try {
            latch.await();
        } catch (InterruptedException e) {
            log.error(e.getMessage());
        }
        ExecutorServiceUtil.shutdown(executorService);

    }

    private void dummyParse(int i, CountDownLatch latch) {
        dummyParse(i);
        latch.countDown();//计数器
    }

    private void loop() {
        for (int i = 0; i < L0G_FILE_COUNT; i++) {
            dummyParse(i);
        }
    }

    private void dummyParse(int i) {
        try {
            val r = rnd(10, 50);
            if (log.isDebugEnabled()) log.debug("{} => sleep {} ms", i, r);
            Thread.sleep(r);
        } catch (InterruptedException e) {
            log.error(e.getMessage());
        }
    }

    private int rnd(int min, int max) {
        return random.nextInt(max - min + 1) + min;
    }

    private final int L0G_FILE_COUNT = 256;

    private final Random random = new Random(System.currentTimeMillis());


//    public void run(String... args) throws Exception {
//        LogParser logParser = new LogParserWithLoopImpl();
//        long start = System.currentTimeMillis();
//        if (log.isInfoEnabled()) log.info("Hello World!");
//        val folderName = "C:\\Users\\huawei\\Desktop\\sony_practice\\nginx-logs";
//        val logItems = logParser.parse(folderName);
//        if (log.isInfoEnabled()){
//            log.info("elapsed {}ms", System.currentTimeMillis() - start);
//            log.info("files : {}", logItems.size());
//            log.info("total log items : {}", logItems.values().stream().mapToLong(Collection::size).sum());
//            log.info("valid log items : {}", logItems.values().stream().flatMap(Collection::stream).filter(LogItem::isValidRequest).count());
//        }

//    }
}
